package com.keur.money.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.adapter.MullaOnItemChildClickListener
import com.keur.money.base.adapter.MullaRecyclerViewAdapter
import com.keur.money.config.KeurConfigs
import com.keur.money.databinding.PopupMullaCommonBinding
import com.keur.money.utils.popup.EasyPopup
import com.keur.money.utils.popup.XGravity
import com.keur.money.utils.popup.YGravity
import org.json.JSONObject
import com.keur.money.base.adapter.MullaViewHolderHelper as MullaViewHolderHelper1

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/6/18 14:46
 * @Annotation：列表选择
 */
class StepCommonPopup(val context: Context) {
    lateinit var binding: PopupMullaCommonBinding
    //开始选择时间
    private var startTime = 0L;

    /* 筛选框宽度，默认屏幕宽度 */
    var mWindowWidth = MyApp.app.getPhoneWidth()
    //选中的item
    var mSelectItem: JSONObject? = null

    private var mItemListener: IItemClickListener? = null

    interface IItemClickListener {
        fun onConfirmAction(data: JSONObject?)
        fun onDismissListener()
    }
    private var mEasyPopup: EasyPopup? = null

    //分类
    private var mAdapter: BrandAdapter? = null

    fun show(view: View, dimView: ViewGroup) {
        startTime = System.currentTimeMillis()
        if (mEasyPopup == null) {
            showViewDown(view, dimView)
        } else {
            mEasyPopup?.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        }
    }

    /* 选择回调 */
    fun setSelectItemListener(listener: IItemClickListener?) {
        mItemListener = listener
    }

    /* 设置宽度 */
    fun setWindowWidth(width: Int){
        this.mWindowWidth = width
    }

    /* 设置分类数据 */
    fun setBrandList(list: List<JSONObject>) {
        this.mAdapter?.data = list
    }

    private fun showViewDown(view: View, dimView: ViewGroup) {
        mEasyPopup = EasyPopup(context)
        binding = PopupMullaCommonBinding.inflate(LayoutInflater.from(context))
        mEasyPopup?.setContentView(binding.root)
        mEasyPopup?.width = this.mWindowWidth
//        mEasyPopup?.height = Apps.getPhoneHeight() / 2
        mEasyPopup?.setAnimationStyle(R.style.popup_anim)
        //是否允许点击PopupWindow之外的地方消失
        mEasyPopup?.setFocusAndOutsideEnable(true)
        //允许背景变暗
        mEasyPopup?.setBackgroundDimEnable(false)
        //变暗的透明度(0-1)，0为完全透明
        mEasyPopup?.setDimValue(0.4f)
        //变暗的背景颜色
        mEasyPopup?.setDimColor(Color.BLACK)
        //指定任意 ViewGroup 背景变暗
        mEasyPopup?.setDimView(dimView)
//        mEasyPopup?.apply();
        EasyPopup.create()
        mEasyPopup?.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        //排序列表
        initRecyclerView()
        mEasyPopup?.setOnDismissListener { // popupWindow隐藏时恢复屏幕正常透明度
            if (mItemListener != null) {
                mItemListener?.onDismissListener()
            }
        }
    }

    private fun initRecyclerView() {
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        this.mAdapter = BrandAdapter( binding.recyclerView)
        binding.recyclerView.adapter = this.mAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        this.mAdapter?.setOnItemChildClickListener(onItemClickListener()) //点击
    }

    /* RecyclerView点击Item */
    internal inner class onItemClickListener : MullaOnItemChildClickListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onItemChildClick(parent: ViewGroup, childView: View, position: Int) {
            mAdapter?.let {
                val model = it.getItem(position)
                if (childView.id == R.id.itemLayout) {
                    for (brandItem in it.data) {
                        brandItem.put("isSelect",false)
                        if (brandItem?.optString("title") == model.optString("title")) {
                            brandItem.put("isSelect",true)
                        }
                    }
                    it.notifyDataSetChanged()
                    if (mItemListener != null) {
                        model.put("duration",System.currentTimeMillis() - startTime)
                        mItemListener?.onConfirmAction(model)
                        mEasyPopup?.dismiss()
                    }
                }
            }
        }
    }

    internal inner class BrandAdapter(recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.popup_mulla_common_item) {
        override fun setItemChildListener(helper: MullaViewHolderHelper1, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }

        override fun fillData(helper: MullaViewHolderHelper1, position: Int, model: JSONObject) {
            val textTitle = helper.getTextView(R.id.text_title)
            var showTitle = model.optString("title")
            if (KeurConfigs.isSwahili()){
                showTitle = model.optString("title_sw")
            }
            textTitle.text = showTitle

            val itemLayout = helper.getView<LinearLayout>(R.id.itemLayout)
            if (model.optBoolean("isSelect",false)) {
                helper.setVisibility(R.id.img_select,View.VISIBLE)
//                itemLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.FFB4D4FF))
                itemLayout.setBackgroundColor(Color.parseColor("#308027FF"))
            } else {
                helper.setVisibility(R.id.img_select,View.INVISIBLE)
                itemLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white))
            }
            if (position == mData.size - 1){
                helper.setVisibility(R.id.v_line,View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.v_line,View.INVISIBLE)
            }
        }
    }
}